package com.ruoyi.shop.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ShopSourceTypeEnums;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.shop.domain.ShopPointsOrders;
import com.ruoyi.shop.domain.ShopProducts;
import com.ruoyi.shop.mapper.ShopPointsOrdersMapper;
import com.ruoyi.shop.service.IShopPointsIncomeService;
import com.ruoyi.shop.service.IShopPointsOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 积分兑换订单Service业务层处理
 * 
 * @author liuwp
 * @date 2025-11-24
 */
@Service
public class ShopPointsOrdersServiceImpl implements IShopPointsOrdersService 
{
    @Autowired
    private ShopPointsOrdersMapper shopPointsOrdersMapper;
    @Autowired
    private IShopPointsIncomeService shopPointsIncomeService;

    /**
     * 查询积分兑换订单
     * 
     * @param id 积分兑换订单主键
     * @return 积分兑换订单
     */
    @Override
    public ShopPointsOrders selectShopPointsOrdersById(Long id)
    {
        return shopPointsOrdersMapper.selectShopPointsOrdersById(id);
    }

    /**
     * 查询积分兑换订单列表
     * 
     * @param shopPointsOrders 积分兑换订单
     * @return 积分兑换订单
     */
    @Override
    public List<ShopPointsOrders> selectShopPointsOrdersList(ShopPointsOrders shopPointsOrders)
    {
        return shopPointsOrdersMapper.selectShopPointsOrdersList(shopPointsOrders);
    }

    /**
     * 根据业务主键查询一条记录：积分兑换订单
     *
     * @param shopPointsOrders 积分兑换订单
     * @return 积分兑换订单
     */
    @Override
    public ShopPointsOrders selectOneShopPointsOrders(ShopPointsOrders shopPointsOrders)
    {
        return shopPointsOrdersMapper.selectOneShopPointsOrders(shopPointsOrders);
    }

    /**
     * 新增积分兑换订单
     * 
     * @param shopPointsOrders 积分兑换订单
     * @return 结果
     */
    @Override
    public int insertShopPointsOrders(ShopPointsOrders shopPointsOrders)
    {
        return shopPointsOrdersMapper.insertShopPointsOrders(shopPointsOrders);
    }

    /**
     * 修改积分兑换订单
     * 
     * @param shopPointsOrders 积分兑换订单
     * @return 结果
     */
    @Override
    public int updateShopPointsOrders(ShopPointsOrders shopPointsOrders)
    {
        return shopPointsOrdersMapper.updateShopPointsOrders(shopPointsOrders);
    }

    @Override
    public int updateShopPointsOrdersCondition(ShopPointsOrders shopPointsOrders, ShopPointsOrders shopPointsOrdersCondition)
    {
        return shopPointsOrdersMapper.updateShopPointsOrdersCondition(shopPointsOrders, shopPointsOrdersCondition);
    }

    /**
     * 批量删除积分兑换订单
     * 
     * @param ids 需要删除的积分兑换订单主键
     * @return 结果
     */
    @Override
    public int deleteShopPointsOrdersByIds(Long[] ids)
    {
        return shopPointsOrdersMapper.deleteShopPointsOrdersByIds(ids);
    }

    /**
     * 删除积分兑换订单信息
     * 
     * @param id 积分兑换订单主键
     * @return 结果
     */
    @Override
    public int deleteShopPointsOrdersById(Long id)
    {
        return shopPointsOrdersMapper.deleteShopPointsOrdersById(id);
    }

    /**
     * 条件删除积分兑换订单
     *
     * @param shopPointsOrders 积分兑换订单
     * @return 结果
     */
    @Override
    public int deleteShopPointsOrdersByCondition(ShopPointsOrders shopPointsOrders)
    {
        return shopPointsOrdersMapper.deleteShopPointsOrdersByCondition(shopPointsOrders);
    }

    @Override
    @Transactional   // TODO 事务这样写好像不太对 再说吧
    public AjaxResult exchangeShopPointsOrders(ShopPointsOrders shopPointsOrders, ShopProducts shopProducts) {
        // 创建订单
        shopPointsOrders.setOrderNo("SO"+ IdUtils.getCheckCode(15, true));
        shopPointsOrders.setTotalPoints(shopPointsOrders.getProductCount() * shopProducts.getPointsPrice());
        shopPointsOrders.setActualPoints(shopPointsOrders.getTotalPoints());
        AjaxResult result = shopPointsIncomeService.pointsOperationRecord(shopPointsOrders.getUserId(), -shopPointsOrders.getActualPoints(), ShopSourceTypeEnums.EXCHANGE, "商城兑换", shopPointsOrders.getOrderNo());
        if (result.get("code").equals(200)){
            insertShopPointsOrders(shopPointsOrders);
            return AjaxResult.success(shopPointsOrders);
        }else {
            return result;
        }
    }

}
